//	AUTHOR:			ENRICO CANTONI
//	CREATED:		DECEMBER 15, 2017
//	MODIFIED: 		NOVEMBER 9, 2022
//	DESCRIPTION:	Makes descriptive plots (Figures 1, 2, A1, A2, and A4).


// Set Stata version
version 16.1

********************************************************************************
********************************************************************************

*** FIGURE 1A: MEAN TURNOUT BY GENDER AND AGE, CPS DATA 2008-2018 ***

// Load data
use ${CPS_clean} if inrange(age, 18, 80)
replace mn_voted = mn_voted*100

// Make graph for f-m difference
cd ${output_graphs}
#delimit ;
twoway	(line mn_voted age if sex == "Female":sex_lbl, lcolor(orange) sort)
		(line mn_voted age if sex == "Male":sex_lbl, lcolor(navy) sort),
		graphregion(fcolor(white) lcolor(white) margin(zero))
		legend(order(1 "Women" 2 "Men") cols(1) position(5) ring(0))
		xtitle(Age)
		ylabel(20(10)70, nogrid angle(horizontal))
		ytitle("Average Voter Turnout (%)")
		saving(Figure_1_a.gph, replace)
;
#delimit cr
graph export Figure_1_a.pdf, replace
clear

********************************************************************************
********************************************************************************

*** FIGURE 1B: MEAN TURNOUT BY GENDER AND AGE ***

// Load data
use ${analysis_clean} if s_all & inrange(age, 18, 80)

// Create average turnout by age
gcollapse (mean) mn_voted = voted, by(age female)
replace mn_voted = mn_voted*100

// Make graph for f-m difference
cd ${output_graphs}
#delimit ;
twoway	(line mn_voted age if female, lcolor(orange) sort)
		(line mn_voted age if !female, lcolor(navy) sort),
		graphregion(fcolor(white) lcolor(white) margin(zero))
		legend(order(1 "Women" 2 "Men") cols(1) position(5) ring(0))
		xtitle(Age)
		ylabel(70(5)95, nogrid angle(horizontal))
		ytitle("Average Voter Turnout (%)")
		saving(Figure_1_b.gph, replace)
;
#delimit cr
graph export Figure_1_b.pdf, replace
clear

********************************************************************************
********************************************************************************

*** FIGURE 2: TURNOUT BY INCOME, GENDER, AND MARITAL STATUS ***

// Load data
use ${analysis_clean} if s_all & age <= 80

// Keep only never-married and married voters
drop if divorced | widowed

// Two-percent bin of income distribution
fasterxtile income_bin = hh_income_mod, nquantiles(20000) by(female married)
gegen temp = mean(hh_income_mod), by(income_bin female married)

// Create average turnout by age
gcollapse (mean) temp voted, by(female income_bin married)
replace voted = voted*100

// Make graphs
cd ${output_graphs}
#delimit ;
twoway	(lowess voted temp if female & !married & temp < 200, lcolor(orange))
		(lowess voted temp if !female & !married & temp < 200, lcolor(navy)),
		graphregion(fcolor(white) lcolor(white) margin(0 1 0 0))
		legend(order(1 "Women" 2 "Men") cols(1) position(5) ring(0))
		xtitle("Household income (2010 EUR*1,000)")
		ylabel(40(10)100, nogrid angle(horizontal))
		ytitle("Average Voter Turnout (%)")
		saving(Figure_2_a.gph, replace)
;
graph export Figure_2_a.pdf, replace;
twoway	(lowess voted temp if female & married & temp < 200, lcolor(orange))
		(lowess voted temp if !female & married & temp < 200, lcolor(navy)),
		graphregion(fcolor(white) lcolor(white) margin(0 1 0 0))
		legend(order(1 "Women" 2 "Men") cols(1) position(5) ring(0))
		xtitle("Household income (2010 EUR*1,000)")
		ylabel(40(10)100, nogrid angle(horizontal))
		ytitle("Average Voter Turnout (%)")
		saving(Figure_2_b.gph, replace)
;
#delimit cr
graph export Figure_2_b.pdf, replace
clear

********************************************************************************
********************************************************************************

*** FIGURE A1: PATTERNS OF DECLINING TURNOUT (FROM KOSTELKA AND BLAIS, 2021) ***

use ${data_raw_KostelkaBlais}/Base_dataset, clear

foreach i of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
local k = `i' + 10
gen decade_`i's = year >=`i' & year<`k' 
} 

foreach var of varlist Turnout {
egen a_1940_`var' = mean(`var') if decade_1940s==1, by(country)
egen a_1950_`var' = mean(`var') if decade_1950s==1, by(country)
egen a_1960_`var' = mean(`var') if decade_1960s==1, by(country)
egen a_1970_`var' = mean(`var') if decade_1970s==1, by(country)
egen a_1980_`var' = mean(`var') if decade_1980s==1, by(country)
egen a_1990_`var' = mean(`var') if decade_1990s==1, by(country)
egen a_2000_`var' = mean(`var') if decade_2000s==1, by(country)
egen a_2010_`var' = mean(`var') if decade_2010s==1, by(country)
}

cap drop temp*  
gen temp1 = decade_1940s ==1 
gen temp2 = decade_2000s ==1 
egen temp3 = max(temp1), by(country)
egen temp4 = max(temp2), by(country)
gen filter_1940_2010 = temp3 ==1 & temp4 ==1 
drop temp*

egen decade = group(decade_2010s decade_2000s decade_1990s decade_1980s decade_1970s decade_1960s decade_1950s decade_1940s), label
label define decade 1"1940s" 2"1950s" 3"1960s"  4"1970s"  5"1980s" 6"1990s"  7"2000s" 8"2010s", replace
label values decade decade

sort country year
bysort country decade: gen temp = _n 

su a_* if temp == 1

gen filter_it = country
recode filter_it 51=1 else=0
gen decade_turnout_it =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_it = r(mean) if temp==1 & decade_`v's == 1
}

gen filter_de = country
recode filter_de 37=1 else=0
gen decade_turnout_de =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_de==1
replace decade_turnout_de = r(mean) if temp==1 & decade_`v's == 1
}

gen filter_fr = country
recode filter_fr 34=1 else=0
gen decade_turnout_fr =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_fr==1
replace decade_turnout_fr = r(mean) if temp==1 & decade_`v's == 1
}

gen filter_uk = country
recode filter_uk 116=1 else=0
gen decade_turnout_uk =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_uk==1
replace decade_turnout_uk = r(mean) if temp==1 & decade_`v's == 1
}

gen filter_us = country
recode filter_us 117=1 else=0
gen decade_turnout_us =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_us==1
replace decade_turnout_us = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_astl = country
recode filter_astl 4=1 else=0
gen decade_turnout_astl =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_astl = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_aus = country
recode filter_aus 5=1 else=0
gen decade_turnout_aus =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_aus = r(mean) if temp==1 & decade_`v's == 1
}

gen filter_be = country
recode filter_be 8=1 else=0
gen decade_turnout_be =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_be = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_ca = country
recode filter_ca 16=1 else=0
gen decade_turnout_ca =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_ca = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_dk = country
recode filter_dk 26=1 else=0
gen decade_turnout_dk =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_dk = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_fi = country
recode filter_fi 33=1 else=0
gen decade_turnout_fi =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_fi = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_ie = country
recode filter_ie 49=1 else=0
gen decade_turnout_ie =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_ie = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_is = country
recode filter_is 50=1 else=0
gen decade_turnout_is =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_is = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_lux = country
recode filter_lux 64=1 else=0
gen decade_turnout_lux =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_lux = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_nl = country
recode filter_nl 78=1 else=0
gen decade_turnout_nl =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_nl = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_nz = country
recode filter_nz 79=1 else=0
gen decade_turnout_nz =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_nz = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_no = country
recode filter_no 83=1 else=0
gen decade_turnout_no =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_no = r(mean) if temp==1 & decade_`v's == 1
}
gen filter_ch = country
recode filter_ch 107=1 else=0
gen decade_turnout_ch =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_it==1
replace decade_turnout_ch = r(mean) if temp==1 & decade_`v's == 1
}

egen continuousdem = rmean(decade_turnout_ch decade_turnout_no decade_turnout_nz decade_turnout_nl decade_turnout_lux decade_turnout_is decade_turnout_ie decade_turnout_fi decade_turnout_dk decade_turnout_ca decade_turnout_be decade_turnout_aus decade_turnout_astl decade_turnout_us decade_turnout_uk decade_turnout_fr decade_turnout_de)

tw connected decade_turnout_it decade if country==4 || connected continuousdem decade if country==4, ///
xscale(range(.5(.5)8.5)) xlabel(1"1940s" 2"1950s" 3"1960s" 4"1970s" 5"1980s" 6"1990s" 7"2000s" 8"2010s") xtitle("") ytitle("Voter Turnout (%)") ///
legend(lab(1 "Italy") lab(2 "Other Continuously Democratic Countries")r(2)) graphregion(color(white)) ///
saving(${output_graphs}/Figure_A1.gph, replace)
graph export ${output_graphs}/Figure_A1.pdf, replace
clear

********************************************************************************
********************************************************************************

*** FIGURE A2: TURNOUT IN NATIONAL ELECTIONS BY GENDER, 1992-2018 ***

// Load_data
use peso* regione sesso voto_* using ${data_raw_Cattaneo}/Database1994_2006

// Rename vars
#delimit ;
rename	(voto_94 voto_95 voto_96 voto_00 voto_01 voto_05 voto_06
		 peso94 peso95 peso96 peso00 peso01 peso05 peso06)
		(voto1994 voto1995 voto1996 voto2000 voto2001 voto2005 voto2006
		 peso1994 peso1995 peso1996 peso2000 peso2001 peso2005 peso2006)
;
#delimit cr

// Create person ID
gen temp_id = _n

// Reshape long and drop resulting missings: i(temp_id) j(year)
reshape long voto peso, i(temp_id) j(year)
drop if missing(voto)

// Create mean turnout by year-gender, for Italy and for Emilia-Romagna
gegen mn_voto_it = mean(voto) [aw=peso], by(year sesso)
replace mn_voto_it = mn_voto_it*100
#delimit ;
gegen mn_voto_er = mean(voto) [aw=peso] if regione == "EMILIA ROMAGNA",
	by(year sesso)
;
#delimit cr
gegen temp = mean(mn_voto_er), by(year sesso)
assert temp == mn_voto_er if !mi(mn_voto_er)
replace mn_voto_er = temp*100

// Keep only 1 obs/year-gender
keep year sesso mn_voto*
duplicates drop

// Font type
graph set window fontface "Times New Roman"

// Loop over regional and national elections
local election_types national regional

// Election type-specific parameters
local if_national "inlist(year, 1994, 1996, 2001, 2006)"
local if_regional "inlist(year, 1995, 2000, 2005)"
local xticks_national "1994 1996 2001 2006"
local xticks_regional "1995 2000 2005"
local yrange_national "80(5)95"
local yrange_regional "70(5)95"

// Loop over election types
foreach t of local election_types {

	// Voter turnout by election type and year
	#delimit ;
	twoway	(scatter mn_voto_it year if sesso == "Maschio":SESSO & `if_`t'',
				connect(line) sort(year) msymbol(O) lcolor("51 122 183")
					mcolor("51 122 183"))
			(scatter mn_voto_it year if sesso == "Femmina":SESSO & `if_`t'',
				connect(line) sort(year) msymbol(D) lcolor("217 83 79")
					mcolor("217 83 79"))
			(scatter mn_voto_er year if sesso == "Maschio":SESSO & `if_`t'',
				connect(line) sort(year) msymbol(O) lcolor("49 176 213")
					mcolor("49 176 213"))
			(scatter mn_voto_er year if sesso == "Femmina":SESSO & `if_`t'',
				connect(line) sort(year) msymbol(D) lcolor("240 173 78")
					mcolor("240 173 78")),
			xlabel(`xticks_`t'')
			xtitle("Year")
			ylabel(`yrange_`t'', angle(horizontal) nogrid)
			ytitle("Voter Turnout (%)")
			graphregion(fcolor(white) lcolor(white)
				color(white) margin(0 0 0 0))
			legend(order(2 "Females - Italy"
						 1 "Males - Italy"
						 4 "Females - Emilia-Romagna"
						 3 "Males - Emilia-Romagna")
				region(lcolor(white))
				rows(2) ring(1) position(6)
					region(lcolor(none%0) fcolor(none%0))
				bmargin(zero) size(small) symxsize(medsmall))
			title("`=proper("`t'")' Elections")
			yscale(titlegap(0))
			name(`t', replace)
	;
	#delimit cr
}

// Output name
local o ${output_graphs}/Figure_A2

// Combine graphs
#delimi ;
grc1leg national regional,
	graphregion(fcolor(white) lcolor(white))
	imargin(0 3 0 0)
	graphregion(margin(l=0 r=0 t=0 b=0))
	saving(`o', replace)
	//ycommon
;
#delimit cr

// Export graph
graph export `o'.pdf, replace
clear

********************************************************************************
********************************************************************************

*** FIGURE A4: TURNOUT BY INCOME, GENDER, AND AGE GROUP ***

// Load data
use ${analysis_clean} if s_all & age <= 80

// Keep only never-married and married voters
drop if divorced | widowed

// Create groups of voters
by ID_voter (year): gen agegroup = 1 if age[1] <= 27
by ID_voter (year): replace agegroup = 2 if inrange(age[1], 28, 47)
by ID_voter (year): replace agegroup = 3 if age >= 48

// Two-percent bin of income distribution
fasterxtile income_bin = hh_income_mod, nquantiles(20000) by(female agegroup)
gegen temp = mean(hh_income_mod), by(income_bin agegroup)

// Create average turnout by age
gcollapse (mean) temp voted, by(female income_bin agegroup)
replace voted = voted*100

// Make graphs
cd ${output_graphs}
#delimit ;
twoway	
		(lowess voted temp if female & agegroup == 1 & temp < 200,
			lcolor(gold) lpattern(dash))
		(lowess voted temp if female & agegroup == 2 & temp < 200,
			lcolor("252 141 89") lpattern(dash))
		(lowess voted temp if female & agegroup == 3 & temp < 200,
			lcolor("215 48 39") lpattern(dash))
		(lowess voted temp if !female & agegroup == 1 & temp < 200,
			lcolor("145 191 219"))
		(lowess voted temp if !female & agegroup == 2 & temp < 200,
			lcolor("90 180 172"))
		(lowess voted temp if !female & agegroup == 3 & temp < 200,
			lcolor("69 117 180")),
		graphregion(fcolor(white) lcolor(white) margin(0 1 0 0))
		legend(order(1 "Women Aged 18-27"
					 2 "Women Aged 28-47"
					 3 "Women Aged 48+"
					 4 "Men Aged 18-27"
					 5 "Men Aged 28-47"
					 6 "Men Aged 48+") cols(1) position(5) ring(0))
		xtitle("Household income (2010 EUR*1,000)")
		ylabel(50(10)100, nogrid angle(horizontal))
		ytitle("Average Voter Turnout (%)")
		saving(Figure_A4.gph, replace)
;
#delimit cr
graph export Figure_A4.pdf, replace
clear
